﻿using Newtonsoft.Json;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wpf.Netdisk.Extensions;
using Wpf.Netdisk.Models.BaseModel;
using QueryParameter = Wpf.Netdisk.Models.BaseModel.QueryParameter;

namespace Wpf.Netdisk.Services.Common
{
    public class HttpRestClient
    {
        private readonly string apiUrl;
        protected  RestClient client;

        public HttpRestClient(string apiUrl)
        {
            this.apiUrl = apiUrl;
            client = new RestClient();
        }

        public async Task<ApiResult> ExecuteAsync(BaseRequest baseRequest)
        {
            var request = new RestRequest();
            request.Method = baseRequest.Method;
            request.AddHeader("Content-Type", baseRequest.ContentType);
            if (!string.IsNullOrWhiteSpace(TokenExtensions.token))
            {
                request.AddHeader("Authorization", TokenExtensions.token);
            }

            if (baseRequest.Parameter != null)
            {
                string body = JsonConvert.SerializeObject(baseRequest.Parameter);
                request.AddStringBody(body, ContentType.Json);
            }

            client = new RestClient(apiUrl + baseRequest.Route);
            var response = await client.ExecuteAsync(request);
            if (response.StatusCode == System.Net.HttpStatusCode.OK)
                return JsonConvert.DeserializeObject<ApiResult>(response.Content);
            else
                return  new ApiResult(-1,string.Empty,null) ;
        }
        public async Task<PageViewModel> ExecutePageViewAsync(BaseRequest baseRequest)  
        {
            var request = new RestRequest();
            request.Method = baseRequest.Method;
            request.AddHeader("Content-Type", baseRequest.ContentType);
            if (!string.IsNullOrWhiteSpace(TokenExtensions.token))
            {
                request.AddHeader("Authorization", TokenExtensions.token);
            }

            if (baseRequest.Parameter != null)
            {
                string body = JsonConvert.SerializeObject(baseRequest.Parameter);
                request.AddStringBody(body, ContentType.Json);
            }

            client = new RestClient(apiUrl + baseRequest.Route);
            var response = await client.ExecuteAsync(request);
            if (response.StatusCode == System.Net.HttpStatusCode.OK)
                return JsonConvert.DeserializeObject<PageViewModel>(response.Content);
            else
                return default;
        }

        public async Task<ApiResult<T>> ExecuteAsync<T>(BaseRequest baseRequest)
        {
            var request = new RestRequest();
            request.Method = baseRequest.Method;
            request.AddHeader("Content-Type", baseRequest.ContentType);
            if (!string.IsNullOrWhiteSpace(TokenExtensions.token))
            {
                request.AddHeader("Authorization", TokenExtensions.token);
            }

            if (baseRequest.Parameter != null)
            {
                string body = JsonConvert.SerializeObject(baseRequest.Parameter);
                request.AddStringBody(body, ContentType.Json);
            }

            client = new RestClient(apiUrl + baseRequest.Route);
            var response = await client.ExecuteAsync(request);
            if (response.StatusCode == System.Net.HttpStatusCode.OK)
                return JsonConvert.DeserializeObject<ApiResult<T>>(response.Content);
            else
                return new ApiResult<T>(-1, string.Empty, default);
        }
    }
}
